/**
* @description: 待办工单 -》 通信电源巡视 -》 通信电源核查
**/

<template>
  <view class="power-inspect-main">
    <nav-bar class="header" back title="通信电源核查" @goBack="goBack"/>
    <view class="main-container">
      <!--基本信息-->
      <plateTitle title="基本信息" class="plate-title-1"/>
      <view class="base-info-container">
        <van-cell-group>
          <van-cell v-for="(item,index) in taskDetail.powerDetailArr" :key="index" :title="item.title" :value="item.text">
            <template #right-icon v-if="item.title === '实物ID'">
<!--              <span class="operation"><img src="../../../../static/img/scan.png" alt=""></span>-->
              <span class="operation" v-if="taskDetail.originData.entityid !== null" @click="scanClick">核对</span>
            </template>
          </van-cell>
        </van-cell-group>
      </view>
      
      <!--巡视信息-->
      <plateTitle title="巡视信息" class="plate-title-2"/>
      <view class="patrol-info-container">
        <van-field
          readonly
          clickable
          input-align="right"
          right-icon="arrow"
          :value="isCoincide"
          label="台账信息与现场信息是否一致"
          placeholder="点击选择"
          @click="showPicker = true"
        />
        <van-radio-group v-show="isCoincide === '不一致'"
                         v-model="operation"
                         icon-size="15px"
                         checked-color="#417ADC">
          <van-radio name="noExist">现场无此设备，将标记此设备状态为“不存在”</van-radio>
          <van-divider dashed />
          <van-radio name="modify">现场设备信息不一致，将标记此设备状态为“修改”</van-radio>
        </van-radio-group>
      </view>
      
      <!--按钮-->
      <van-button block @click="confirmInfo">{{ buttonText() }}</van-button>
      
      <!--选择器-->
      <van-popup v-model="showPicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="columns"
          @confirm="onConfirm"
          @cancel="showPicker = false"
        />
      </van-popup>
    </view>
  </view>
</template>

<script>
import moment from "moment";

let power_obj = {
  "equName": "通信电源名称",
  "sitename": "所属站点",
  "parRoom": "所属机房",
  "parRack": "所属机柜编号",
  "parRackScreen": "所属机柜屏眉",
  "serilanum": "通信电源顺序",
  "entityid": "实物ID",
  "powerTechType": "通信电源制造厂商",
  "powerModelnumber": "通信电源型号",
  "powerFullCapacity": "满配整流容量(A)",
  "powerPresentCapacity": "当前配置整流容量(A)",
  "powerRectification": "整流模块型号",
  "powerSingle": "单个整流模块容量(A)",
  "powerBegTim": "通信电源投运时间",
  "accumProducer": "配套蓄电池组制造厂商",
  "accumModelnumber": "配套蓄电池组型号",
  "accumType": "配套蓄电池类型",
  "accumNum": "配套蓄电池组数量",
  "accumBegTim": "蓄电池组投运时间",
  "runState": "运行状态",
  "accumCapacity": "每组蓄电池容量(Ah)",
  "powerLoadCurrent": "当前负载电流(A)",
  "accumSumCapacity": "蓄电池实测总容量(Ah)"
}
import navBar from "../../../../components/common/nav-bar";
import plateTitle from "../../../../components/business/plateTitle";
import {mapGetters} from "vuex";
import {Dialog, Toast} from "vant";
import wx from "../../../../static/common_js/jweixin_1.0.0";
import {getCheckEntityPower} from "../../../../api/apiIndex";
export default {
  components: { navBar, plateTitle },
      name: "powerInspect",
  data() {
    return {
      taskDetail: {},
      buttonText: () => {
        // if(this.isCoincide === "一致") return "提交";
        if(this.operation === "noExist") return "保存";
        if(this.operation === "modify") return "去完善台账信息";
      },
      isCoincide: "不一致",
      showPicker: false,
      columns: ["不一致"],
      operation: "modify",
      objId:'',
      taskSiteId:'', // 返回跳转携带参数
      taskType:'', // 返回跳转携带参数
      taskSiteName:'',
      reTourCheck: '1', // 巡视-1 重新编辑-0
    }
  },
  onLoad (options) {
    this.objId = options.objId;
    this.taskSiteId = options.taskSiteId;
    this.taskSiteName = options.taskSiteName;
    this.taskType = options.state;
    this.getPowerInfo()
  },
  computed:{
    ...mapGetters(['userData'])
  },
  methods: {
    goBack() {
      uni.navigateTo({
        url: `/pages/workBench/todoTask/powerTour/powerTour?taskId=${this.taskDetail.originData.taskId}&taskSiteId=${this.taskSiteId}&state=${this.taskType}&taskSiteName=${this.taskSiteName}`
      })
    },
    scanClick() {
      let _this = this;
      wx.scanQRCode({
        desc:"scanQRCode desc",
        needResult: 1,
        scanType:["qrCode"],
        success:function(res){
          let scanCodeId = res.resultStr
          _this.getCheckEntityP(scanCodeId)
        },
        error:function(res){
          console.log("scanQRCode error",res);
        }
      });
    },
    // 核对
    getCheckEntityP(scanCodeId) {
      let params = {
        objId: this.objId,
        entityId: scanCodeId
      }
      this.$api.getCheckEntityPower(params).then(res => {
        if(res.data.flag === true) {
          Dialog.confirm({
            message: '信息匹配成功',
          }).then(() => {
          })
        }else {
          Dialog.confirm({
            title:'信息不匹配',
            message: '该设备实物ID为：'+ res.data.entityId,
          }).then(() => {
          })
        }
      })
    },
    getPowerInfo() {
      this.$api.todoTaskApi.powerDetail({objId: this.objId}).then(res => {
        this.taskDetail = {
          originData: res.data,
          powerDetailArr: Object.keys(power_obj).map(item => {
            return {
              title: power_obj[item],
              text: res.data[item]
            }
          })
        };
      })
    },
    onConfirm(value) {
      this.isCoincide = value;
      this.showPicker = false;
    },
    confirmInfo() {
      // 不一致
      if (this.operation === "noExist") { // 认定设备不存在时进行保存
        Dialog.confirm({
          message: "所选巡视结果是否确认？确认后不可变更，如有问题请联系运维人员。",
        }).then(() => {
          let checkExistParams = {
            "taskId": this.taskDetail.originData.taskId,
            "objId": this.objId,    //巡视设备ID    必填
            "tourOperId": this.userData.userId,    //巡视人ID    必填    userId放进来
            "tourOperName": this.userData.userName,    //巡视人名称    必填    userName放进来
            "tourTim": moment(new Date()).format("YYYY-MM-DD HH:mm:ss"),  //巡视时间 必填
            "reTourCheck": this.reTourCheck, // 巡视-1 重新编辑-0
          };
          this.$api.todoTaskApi.infomationNotFound(checkExistParams).then(res => {
            if (res.code === 0) {
              // 如果当前为巡视，且返回data为0，表示重复巡视
              if (this.reTourCheck === "1" && res.data === 0) {
                Dialog.confirm({
                  message: "该电源已被巡视，请返回重试",
                }).then(() => {
                  uni.navigateTo({
                    url: `/pages/workBench/todoTask/powerTour/powerTour?taskId=${this.taskId}&taskSiteId=${this.taskSiteId}&state=${this.taskType}&taskSiteName=${this.taskSiteName}`
                  });
                });
              } else {
                Toast.success({
                  message: '保存成功',
                  onOpened: () => {
                    uni.navigateTo({
                      url: `/pages/workBench/todoTask/powerTour/powerTour?taskId=${this.taskDetail.originData.taskId}&taskSiteId=${this.taskSiteId}&state=${this.taskType}&taskSiteName=${this.taskSiteName}`
                    });
                  }
                });
              }
            } else {
              Toast.fail("操作失败");
            }
    
          });
        }).catch(() => {})
      }
      // 修改
      if (this.operation === 'modify') { // 认定设备存在但是需要完善信息
        uni.navigateTo({
          url: `/pages/workBench/todoTask/powerTour/completePower?type=update&objId=${this.objId}&taskId=${this.taskDetail.originData.taskId}&taskSiteId=${this.taskSiteId}&state=${this.taskType}&taskSiteName=${this.taskSiteName}`
        })
      }
    }
  }
};
</script>

<style scoped lang="scss">
.power-inspect-main {
  .header {
    position: sticky;
    top: 0;
    z-index: 9;
  }
  .main-container {
    min-height: calc(100vh - #{px2vh(283)});
    padding: px2vh(30) px2vw(30) px2vh(90) px2vw(30);
    background-color: #F8F8F8;
    /deep/.van-field__label, 
    /deep/.van-field__control:read-only, 
    /deep/.van-field__body .van-field__control,
    /deep/.van-cell-group .van-cell
    {
      font-size: px2vw(42);
    }
    /deep/.van-field__label {
      width: auto;
    }
    
    .plate-title-1 {
      margin-top: px2vh(30);
      margin-bottom: px2vh(27);
    }
    
    .base-info-container {
      border-radius: $border-radius-base;
      overflow: hidden;
      background-color: #FFFFFF;
  
      .operation {
        margin-left: px2vw(30);
        color: #417ADC;
        img {
          width: px2vw(42);
          vertical-align: text-top;
        }
      }
    }
    
    .plate-title-2 {
      margin-top: px2vh(60);
      margin-bottom: px2vh(27);
    }
    
    .patrol-info-container {
      font-size: px2vw(42);
      color: #666666;
      border-radius: $border-radius-base;
      overflow: hidden;
      background-color: #FFFFFF;
      
      .van-radio-group {
        padding: px2vh(60) px2vw(42);
      }
      
      /deep/.van-field__label {
        width: auto;
      }
      ///deep/.van-icon-success::before {
      //  content: "\F030"
      //}
    }
    
    /deep/.van-button {
      margin-top: px2vh(100);
      height: px2vh(120);
      color: #fff;
      background-color: #417ADC;
      border: 1px solid #417ADC;
      border-radius: px2vh(60);
    }
  }
}
</style>